﻿Imports System.Web
Imports System.Text.RegularExpressions

Namespace DNNCorp.Web
    Public NotInheritable Class WebFilterModule
        Implements IHttpModule
#Region "Constants"
        Private Const TEXTHTMLCONTENT As String = "text/html"
        Private Const FILTERINSTALLED As String = "TAG.httpfilter.installed"
        Private Shared ReadOnly FilterInstalledObject As Object = New Object
#End Region
        Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
        End Sub

        Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
            AddHandler context.ReleaseRequestState, AddressOf AddFilter
        End Sub

        Private Sub AddFilter(ByVal sender As Object, ByVal e As EventArgs)
            Dim app As HttpApplication = CType(sender, HttpApplication)

            If Not app.Context.Items.Contains(FILTERINSTALLED) Then
                app.Context.Items.Add(FILTERINSTALLED, FilterInstalledObject)

                If app.Response.ContentType = TEXTHTMLCONTENT Then
                    app.Response.Filter = New WebFilterStream(app.Response.Filter)
                End If
            End If

        End Sub

    End Class
End Namespace
